<?php
namespace App\Pay;
use Carbon\Carbon;
use Youzan\Open\Token;
use Youzan\Open\Client;

class Youzan
{
    private static $api_version = '3.0.0';
    private static $api_version_4 = '4.0.0';
    private static $method_create_qr = 'youzan.pay.qrcode.create';
    private static $method_get_qr_trades = 'youzan.trades.qr.get';
    private static $method_get_one_trade = 'youzan.trade.get';


    private static $QR_TYPE_NOLIMIT = 'QR_TYPE_NOLIMIT';
    private static $QR_TYPE_FIXED_BY_PERSON = 'QR_TYPE_FIXED_BY_PERSON';
    private static $QR_TYPE_DYNAMIC = 'QR_TYPE_DYNAMIC';

    public function construct()
    {

    }

    public static function createQRCode($price)
    {
        $client_id = config('youzan.client_id');
        $client_secret = config('youzan.client_secret');
        $keys['kdt_id'] = config('youzan.kdt_id');
        $type = config('youzan.type');
        $youzan_token = session('youzan_token',null);
        //判断token是否过期
        if(isset($youzan_token['tm_start']) && isset($youzan_token['expires_in']) && (time() - $youzan_token['tm_start'] < $youzan_token['expires_in']))
        {
            $access_token = $youzan_token;
        }
        else
        {
            $access_token = (new Token($client_id,$client_secret))->getToken($type,$keys);
            $access_token['tm_start'] = time();
            session(['youzan_token' => $access_token]);
        }

        $client = new Client($access_token['access_token']);
        $params = [
            'qr_name' => 'D币充值',
            'qr_price' => $price * 100,
            'qr_type'  => Youzan::$QR_TYPE_DYNAMIC];
        $res = $client->get(Youzan::$method_create_qr, Youzan::$api_version, $params);
        $res = $res['response'];
        return $res;

    }

    public static function getTradeList($params = [])
    {
        $client_id = config('youzan.client_id');
        $client_secret = config('youzan.client_secret');
        $keys['kdt_id'] = config('youzan.kdt_id');
        $type = config('youzan.type');
        $youzan_token = session('youzan_token',null);
        //判断token是否过期
        if(isset($youzan_token) && time() - $youzan_token['tm_start'] < $youzan_token['expires_in'])
        {
            $access_token = $youzan_token;
        }
        else
        {
            $access_token = (new Token($client_id,$client_secret))->getToken($type,$keys);
            $access_token['tm_start'] = time();
            session(['youzan_token' => $access_token]);
        }

        $client = new Client($access_token['access_token']);

        $res = $client->get(Youzan::$method_get_qr_trades, Youzan::$api_version, $params);
        return $res['response'];
        //var_dump($res['response']);
    }

    /*
     *
     */
    public static function getSpecifiedTrade($params)
    {
        return Youzan::getTradeList($params);
    }

    /*
     *
     */
    public static function getOneTradeByTid($tid)
    {
        $client_id = config('youzan.client_id');
        $client_secret = config('youzan.client_secret');
        $keys['kdt_id'] = config('youzan.kdt_id');
        $type = config('youzan.type');
        $youzan_token = session('youzan_token',null);
        //
        if(isset($youzan_token) && time() - $youzan_token['tm_start'] < $youzan_token['expires_in'])
        {
            $access_token = $youzan_token;
        }
        else
        {
            $access_token = (new Token($client_id,$client_secret))->getToken($type,$keys);
            $acc判断token是否过期ess_token['tm_start'] = time();
            session(['youzan_token' => $access_token]);
        }

        $client = new Client($access_token['access_token']);

        $params = ['tid' => $tid];

        $res = $client->get(Youzan::$method_get_one_trade, Youzan::$api_version_4, $params);

        return $res['response'];

    }

}